package com.wjj.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.wjj.common.core.utils.MapstructUtils;
import com.wjj.common.core.utils.StringUtils;
import com.wjj.common.mybatis.core.page.PageQuery;
import com.wjj.common.mybatis.core.page.TableDataInfo;
import com.wjj.system.domain.SysNotice;
import com.wjj.system.domain.SysUser;
import com.wjj.system.domain.bo.SysNoticeBo;
import com.wjj.system.domain.vo.SysNoticeVo;
import com.wjj.system.domain.vo.SysUserVo;
import com.wjj.system.mapper.SysNoticeMapper;
import com.wjj.system.mapper.SysUserMapper;
import com.wjj.system.service.ISysNoticeService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 公告 服务层实现
 *
 * @author Lion Li
 */
@RequiredArgsConstructor
@Service
public class SysNoticeServiceImpl extends ServiceImpl<SysNoticeMapper, SysNotice> implements ISysNoticeService {

    private final SysUserMapper userMapper;

    @Override
    public TableDataInfo<SysNoticeVo> selectPageNoticeList(SysNoticeBo notice, PageQuery pageQuery) {
        QueryWrapper qw = buildQueryWrapper(notice);
        Page<SysNotice> page = mapper.paginate(pageQuery.build(), qw);
        Page<SysNoticeVo> result = new Page<>(page.getPageNumber(), page.getPageSize(), page.getTotalRow());
        result.setRecords(MapstructUtils.convert(page.getRecords(), SysNoticeVo.class));
        return TableDataInfo.build(result);
    }

    /**
     * 查询公告信息
     *
     * @param noticeId 公告ID
     * @return 公告信息
     */
    @Override
    public SysNoticeVo selectNoticeById(Long noticeId) {
        SysNotice sysNotice = mapper.selectOneById(noticeId);
        return MapstructUtils.convert(sysNotice, SysNoticeVo.class);
    }

    /**
     * 查询公告列表
     *
     * @param notice 公告信息
     * @return 公告集合
     */
    @Override
    public List<SysNoticeVo> selectNoticeList(SysNoticeBo notice) {
        QueryWrapper qw = buildQueryWrapper(notice);
        List<SysNotice> sysNotices = mapper.selectListByQuery(qw);
        return MapstructUtils.convert(sysNotices, SysNoticeVo.class);
    }

    private QueryWrapper buildQueryWrapper(SysNoticeBo bo) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.like(SysNotice::getNoticeTitle, bo.getNoticeTitle(), StringUtils.isNotBlank(bo.getNoticeTitle()))
                .eq(SysNotice::getNoticeType, bo.getNoticeType(), StringUtils.isNotBlank(bo.getNoticeType()));

        if (StringUtils.isNotBlank(bo.getCreateByName())) {
            SysUser sysUser = userMapper.selectOneByQuery(QueryWrapper.create().eq(SysUser::getUserName, bo.getCreateByName()));
            wrapper.eq(SysNotice::getCreateBy, ObjectUtil.isNotNull(sysUser) ? sysUser.getUserId() : null);
        }
        wrapper.orderBy(SysNotice::getNoticeId, true);
        return wrapper;
    }

    /**
     * 新增公告
     *
     * @param bo 公告信息
     * @return 结果
     */
    @Override
    public int insertNotice(SysNoticeBo bo) {
        SysNotice notice = MapstructUtils.convert(bo, SysNotice.class);
        return mapper.insert(notice);
    }

    /**
     * 修改公告
     *
     * @param bo 公告信息
     * @return 结果
     */
    @Override
    public int updateNotice(SysNoticeBo bo) {
        SysNotice notice = MapstructUtils.convert(bo, SysNotice.class);
        return mapper.update(notice);
    }

    /**
     * 删除公告对象
     *
     * @param noticeId 公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeById(Long noticeId) {
        return mapper.deleteById(noticeId);
    }

    /**
     * 批量删除公告信息
     *
     * @param noticeIds 需要删除的公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeByIds(Long[] noticeIds) {
        return this.removeByIds(Arrays.asList(noticeIds)) ? 1 : 0;
    }
}
